package ModelLayer;
import java.util.ArrayList;
/**
 * Write a description of class DvdContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DvdContainer
{
    // instance variables - replace the example below with your own
    private ArrayList<Dvd> myDvds;
    private static DvdContainer instance;

    /**
     * Constructor for objects of class DvdContainer
     */
    private DvdContainer()
    {
        myDvds = new ArrayList<Dvd>();
    }
    
    /**
     * Singleton for the DvdContainer class
     */
    public static DvdContainer getInstance()
    {
        if(instance == null)
        {
            instance = new DvdContainer();
            return instance;
        }
        return instance;
    }
    
    /**
     * This method adds a Dvd to the arraylist
     */
    public void addDvd(String genre, String title, String description, String id)
    {
        
        Dvd d = new Dvd(genre, title, description, id);
        myDvds.add(d);
        
    }
    
    /**
     * This method takes a String input, and searches the arraylist for the name
     */
    public Dvd findDvd(String id)
    {
        boolean found = false;
        int index = 0;
        int theIndex = 0;
        Dvd foundDvd;
        while(index < myDvds.size()-1 && !found)
        {
            if(myDvds.get(index).getId().equals(id))
            {
                theIndex = index;
            }
            index++;
        }
        foundDvd = myDvds.get(theIndex);
        
        return foundDvd;
    }
    
     /**
     * This method takes a string input and alters the instance variable.
     */
    public void setGenre(String id, String newGenre)
    
    {
       boolean found = false;
       int index = 0;
       while(index <= myDvds.size()-1 && !found)
       {
           if(myDvds.get(index).getId().equals(id))
          {
           myDvds.get(index).setGenre(newGenre);
          }
          index ++;
       }
    }
    
      /**
     * This method takes a string input and alters the instance variable.
     */
    public void setTitle(String id, String newTitle)
    
    {
       boolean found = false;
       int index = 0;
       while(index <= myDvds.size()-1 && !found)
       {
           if(myDvds.get(index).getId().equals(id))
          {
           myDvds.get(index).setTitle(newTitle);
          }
          index ++;
       }
    }
    
        /**
     * This method takes a string input and alters the instance variable.
     */
    public void setDescription(String id, String newDescription)
    
    {
       boolean found = false;
       int index = 0;
       while(index <= myDvds.size()-1 && !found)
       {
           if(myDvds.get(index).getId().equals(id))
          {
           myDvds.get(index).setDescription(newDescription);
          }
          index ++;
       }
    }
    
    /**
     * This method takes a string input and searches the arraylist for the dvd, and if found it
     * will delete the dvd from the list.
     */
    public void deleteDvd(String id)
    {
        boolean found = false;
        int index = 0;
        while(index <= myDvds.size()-1 && !found)
        {
            if(myDvds.get(index).getId().equals(id))
            {
                myDvds.remove(index);
                found = true;
            }
            index++;
        }
        if(index == myDvds.size()-1 && !found)
        {
            System.out.println("Id not found");
        } 
    }
    
    /**
     * This method will find the specific Dvd and make a copy of it
     */
    public void createCopy(String id, String copyNr, String year, double price)
    {
        boolean found = false;
        int index = 0;
        while(index < myDvds.size() && !found)
        {
            Dvd d = myDvds.get(index);
            if(d.getId().equals(id))
            {
             d.createCopy(copyNr, year, price);
             found = true;
            }
            index++;
        }
    }
    
    public void printDvdInformation(String id)
    {
        boolean found = false;
        int index = 0;
        while(index < myDvds.size() && !found)
        {
            Dvd d = myDvds.get(index);
            if(d.getId().equals(id))
            {
                d.printInformation();
                found = true;
            }
            index++;
        }
    }
    
    public DvdCopy findCopy(String id, String copyNr)
    {
        int index = 0;
        boolean found = false;
        int theIndex = 0;
        DvdCopy foundCopy = null;
        while(index < myDvds.size() && !found)
        {
            Dvd d = myDvds.get(index);
            DvdCopy dc = d.findCopy(copyNr);
            if(d.getId().equals(id) && 
            dc.getCopyNr().equals(copyNr))
            {
                foundCopy = myDvds.get(index).findCopy(copyNr);
                found = true;
            }
            index++;
        }
        return foundCopy;
    }
    
    
    

    
    public void printAllDvds()
    {
        for(Dvd element : myDvds)
        {
            element.printInformation();
        }
    }
}










